package com.united.ucts.common.exception;

import com.united.frame.common.module.result.Result;
import com.united.frame.plugin.restfull.response.ResponseResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义的全局处理Oauth2抛出的异常
 * 此类继承自框架层的AbstractUnitedExceptionHandler,实现全局异常拦截处理
 *
 * @author Hudan
 * @version 1.0
 * @filename UctsExceptionHandler.java
 * @copyright <a href="www.united.cn">www.united.cn</a>
 * @date 2019-10-26
 */
@Slf4j
@RestControllerAdvice
public class UctsExceptionHandler extends GlobalExceptionHandler {

    /**
     * 处理UCTS的授权业务定义的异常
     *
     * @param e 授权业务定义的异常
     * @return 异常处理结果
     */
    @ResponseResultBody
    @ExceptionHandler(value = OAuth2Exception.class)
    public Result<?> handleOauth2(OAuth2Exception e) {
        return Result.failure(e.getMessage());
    }

    /**
     * 处理账户禁用异常
     *
     * @param e 账户禁用异常
     * @return 异常处理结果
     */
    @ResponseResultBody
    @ExceptionHandler(value = DisabledException.class)
    public Result<?> handle(DisabledException e) {
        return Result.failure(e.getMessage());
    }

    /**
     * 处理账户锁定异常
     *
     * @param e 账户锁定异常
     * @return 异常处理结果
     */
    @ResponseResultBody
    @ExceptionHandler(value = LockedException.class)
    public Result<?> handle(LockedException e) {
        return Result.failure(e.getMessage());
    }

    /**
     * 处理账户过期异常
     *
     * @param e 账户过期异常
     * @return 异常处理结果
     */
    @ResponseResultBody
    @ExceptionHandler(value = CredentialsExpiredException.class)
    public Result<?> handle(CredentialsExpiredException e) {
        return Result.failure(e.getMessage());
    }
}